package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }

    /**
     * 创建床位
     *
     * @param dto 床位信息
     * @return 无返回
     */
    @ApiOperation(value = "新增床位",notes ="传入床位对象" )
    @ApiImplicitParam(name="dto",value = "床位dto对象",dataType = "BedDto")
    @ApiOperationSupport(includeParameters = {"dto.bedNumber","dto.roomId","dto.sort"})
    @PostMapping("/create")
    public ResponseResult createBed(@RequestBody BedDto dto) {
        try {
            bedService.createBed(dto);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
        return success();
    }

    /**
     * 根据id查询
     *
     * @param id 床位id
     * @return 返回床位信息
     */
    @GetMapping("/read/{id}")

    public ResponseResult<BedVo> queryById(@PathVariable Integer id) {
        BedVo bedVo = bedService.queryById(id);
        return success(bedVo);
    }

    /**
     * 更新床位
     *
     * @param dto 床位信息
     * @return 成功
     */
    @PutMapping("/update")
    public ResponseResult updateBed(@RequestBody BedDto dto) {
        bedService.updateBed(dto);
        return success();
    }

    /**
     * 删除床位
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除床位",notes = "根据传入的床位主键ID删除")
    public ResponseResult deleteById(@ApiParam(value = "床位主键ID",required = true)@PathVariable Long id) {
        try {
            bedService.deleteById(id);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_DELETE_FAIL);
        }
        return success();
    }
}
